const book = E = {
	page(_pageNo, _pageSize, name, phone, no, wname, state) {
		let _tbody = $('#_tbody');
		$.ajax({
			url: url.path + 'dingdan/list',
			type: 'get',
			data: {
				pageNo: _pageNo,
				pageSize: _pageSize,
				name: name,
				phone: phone,
				no: no,
				wname: wname,
				state: state
			},
			dataType: 'json',
			success: function(res) {
				console.log(res);
				if (res.code == 200) {
					_tbody.empty(); // 将容器清空
					let list = res.data.dataList;

					$('#p_no').html('').html(res.data.pageNo);
					$('#p_count').html('').html(res.data.totalPageCount);
					$('#zong').html('').html(res.data.totalCount);

					$(list).each(function(i, e) {
						let state;
						if (e.state == 0) {
							state = '已完成';
						} else if (e.state == 1) {
							state = '待派单';
						} else if (e.state == 2) {
							state = '待支付';
						} else if (e.state == 3) {
							state = '处理中';
						} else if (e.state == 4) {
							state = '待审核';
						} else if (e.state == 5) {
							state = '审核未通过';
						} else if (e.state == 6) {
							state = '施工中';
						} else if (e.state == 7) {
							state = '已取消';
						}
						let _tr = $(`<tr></tr>`)
						let _tds = $(`
									<td>` + e.dno + `</td>
	                                <td>` + E.timestampToDate(e.date) + `</td>
									<td>` + e.address + `</td>
									<td>` + e.lxrname + `</td>
									<td>` + e.lxrphone + `</td>
									<td>` + e.description + `</td>
									
									<td><img src="` + e.img1 + `" width="50px">
									<img src="` + e.img2 + `" width="50px">
									<img src="` + e.img3 + `" width="50px">
									</td>
									<td>` + e.wname + `</td>
	                                <td> ` + state + `</td>
	                        `)
						let _cz = $('<td></td>');

						let look = $(
							'<button type="button" class="btn btn-danger btn-xs" style="margin-right:10px">查看</button>'
						);
						// 修改
						let zhipai = $(
							'<button type="button" class="btn btn-warning btn-xs" id="zhipai" >指派</button>'
						);

						let scfy = $(
							'<button type="button" class="btn btn-warning btn-xs" id="scfy" >上传费用</button>'
						);
						let shenhe = $(
							'<button type="button" class="btn btn-warning btn-xs" id="shenhe" >审核</button>'
						);
						let end = $(
							'<button type="button" class="btn btn-warning btn-xs" id="end" >确认完工</button>'
						);
						let update = $(
							'<button type="button" class="btn btn-warning btn-xs" id="update" >修改工单</button>'
						);
						let pj = $(
							'<button type="button" class="btn btn-warning btn-xs" id="pj" >查看评价</button>'
						);


						if (e.state == 0) {
							_cz.append(look).append(pj);
							look.click(function() {
								location.href = "yiwancheng.html?did=" + e.id;
							})
							pj.click(function() {
								location.href = "yiwancheng.html?did=" + e.id;
							})

						} else if (e.state == 1) {
							_cz.append(look).append(zhipai);
							look.click(function() {
								location.href = "daipaidan.html?did=" + e.id;
							})
							zhipai.click(function() {
								location.href = "daipaidan.html?did=" + e.id;
							})
						} else if (e.state == 2) {
							_cz.append(look);
							look.click(function() {
								location.href = "daizhifu.html?did=" + e.id;
							})
						} else if (e.state == 3) {
							_cz.append(look).append(scfy);
							look.click(function() {
								location.href = "jd_chulizhong.html?did=" + e.id;
							})
							scfy.click(function() {
								location.href = "jd_chulizhong.html?did=" + e.id;
							})
						} else if (e.state == 4) {
							_cz.append(look).append(shenhe);
							look.click(function() {
								location.href = "jd_daishenhe.html?did=" + e.id;
							})
							shenhe.click(function() {
								location.href = "jd_daishenhe.html?did=" + e.id;
							})
						} else if (e.state == 5) {
							_cz.append(look).append(update);
							look.click(function() {
								location.href = "shenheno.html?did=" + e.id;
							})
						} else if (e.state == 6) {
							_cz.append(look).append(end);
							look.click(function() {
								location.href = "shigongzhong.html?did=" + e.id;
							})
							end.click(function() {
								location.href = "shigongzhong.html?did=" + e.id;
							})
						} else if (e.state == 7) {
							_cz.append(look);
							look.click(function() {
								location.href = "yiquxiao.html?did=" + e.id;
							})
						}
						_tr.append(_tds).append(_cz);
						_tbody.append(_tr);
					});
				}
			}
		});
	},
	edit(obj, id, state) {
		obj.click(function() {
			if (id != '') {
				$.ajax({
					url: url.path + 'dingdan/edit',
					data: {
						id: id,
						state: state
					},
					type: 'POST',
					dataType: 'json',
					success: function(res) {
						if (res.code == 200) {
							alert(res.msg);
							window.location.reload();
						}
					},
					error: function() {
						console.log("失败")
					}
				})
			}
		})
	},
	// getOne(obj, id) {
	// 	obj.click(function() {
	// 		if (id != '') {
	// 			$.ajax({
	// 				url: 'http://localhost:8081/course/queryOne',
	// 				data: {
	// 					id: id
	// 				},
	// 				type: 'get',
	// 				dataType: 'json',
	// 				success: function(res) {
	// 					console.log(res);
	// 					if (res.code == 200) {
	// 						$('input[name="coursesId"]').val("").val(res.data.coursesId);
	// 						$('input[name="title"]').val("").val(res.data.title);
	// 						$('input[name="duration"]').val("").val(res.data.duration);
	// 						$('input[name="price"]').val("").val(res.data.price);
	// 						$('input[name="description"]').val("").val(res.data.description);
	// 						$('select[name="state"]').val("").val(res.data.state);
	// 						$('input[name="courseUrl"]').val("").val(res.data.courseUrl);
	// 						$('input[name="thumbnailUrl"]').val("").val(res.data.thumbnailUrl);
	// 					}
	// 				},
	// 				error: function() {
	// 					console.log("失败")
	// 				}
	// 			})
	// 		}
	// 	})

	// },
	timestampToDate(timestamp) {
		const date = new Date(timestamp); // 将时间戳转换为Date对象
		const options = {
			year: 'numeric',
			month: 'long',
			day: 'numeric'
		}; // 定义日期格式
		return new Intl.DateTimeFormat('zh-CN', options).format(date); // 使用Intl.DateTimeFormat进行格式化
	},
	first() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $('#chooseSize').val();
		E.page(_pageNo, _size, _lxrname, _lxrphone, _no, _wname, _state);
	},
	tiao() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $('#chooseSize').val();
		E.page(1, _size, _lxrname, _lxrphone, _no, _wname, _state);
	},
	last() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $('#chooseSize').val();
		let _p_count = $('#p_count').html();
		E.page(_pageNo, _size, _lxrname, _lxrphone, _no, _wname, _state);
	},
	prev() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $('#chooseSize').val();
		let _pageNo = $('#p_no').html();
		if (_pageNo > 1) {
			_pageNo--;
			E.page(_pageNo, _size, _lxrname, _lxrphone, _no, _wname, _state);
		} else {
			alert('已经是第一页了')
		}

	},
	next() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $('#chooseSize').val();
		let _pageNo = $('#p_no').html();
		let _p_count = $('#p_count').html();
		if (_pageNo < _p_count * 1) {
			_pageNo++;
			E.page(_pageNo, _size, _lxrname, _lxrphone, _no, _wname, _state);
		} else {
			alert('已经是最后一页了')
		}
	}
}
$(function() {
	E.page(1, 10, '', '', '', '', '');

	$('#chooseSize').change(function() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $(this).val();
		E.page(1, _size, _lxrname, _lxrphone, _no, _wname, _state);
	})

	$('#first').click(function() {
		E.first();
	})

	$('#last').click(function() {
		E.last();
	})

	$('#toPno').blur(function() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _pageNo = $('#toPno').val();
		let _pageSize = $('#chooseSize').val();
		let _p_count = $('#p_count').html();
		if (_pageNo <= _p_count * 1) {
			E.page(1, _size, _lxrname, _lxrphone, _no, _wname, _state);
		} else {
			alert('不能大于总页数')
		}

	})

	$('#x').click(function() {
		let _lxrname = $('#lxrname').val();
		let _lxrphone = $('#lxrphone').val();
		let _wname = $('#wname').val();
		let _no = $('#no').val();
		let _state = $('#state').val();
		let _size = $('#chooseSize').val();
		E.page(1, _size, _lxrname, _lxrphone, _no, _wname, _state);
	})

	$('#btn_search').click(function() {
		let _data = $('#name').val();
		let _size = $('#chooseSize').val();
		E.page(1, _size, _data);
	})

	$('#prev').click(function() {
		E.prev();
	})

	$('#next').click(function() {
		E.next();
	})

})